﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using QuanLyDiem.models;
namespace QuanLyDiem.Controllers
{
    using Library;
    using Library.Data;
    

    public class SYS_NguoiDungCtrl
    {
        public static USERSTATE Login(string Tai_khoan, string Mat_khau, Context db = null)
        {
            if (db == null) db = new Context();

            try
            {
          //      string password = ISecurity.HashData(new System.Security.Cryptography.MD5CryptoServiceProvider(), Mat_khau);

                var users = db.SYS_NguoiDungs.Where(w => w.UserName == Tai_khoan).ToList();

                if (!users.Any()) return USERSTATE.NOTEXIST; //Người dùng không tồn tại

                var user = db.SYS_NguoiDungs.FirstOrDefault(w => w.UserName == Tai_khoan && w.PassWord == Mat_khau);

                if (user == null) return USERSTATE.WRONGPASSWORD; //Sai mật khẩu

                //user.Lan_dang_nhap_cuoi = DbHelper.GetDatabaseDate();
                //user.May_tram = MachineInfo.SystemInformation;

                db.SubmitChanges();
                Program.CurrentUser = user;

                return USERSTATE.AUTHORIZE; //Đăng nhập thành công
            }
            catch (Exception)
            {
            }

            return USERSTATE.ERROR;
        }

        public static void LogOut(Context db = null)
        {
        }
    }
}
